﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "WallTagCalculator.h"
#include "IGenericElement.h"
#include "IGraphicsElementShape.h"
#include "IElementModelShape.h"
#include "IExternalObject.h"
#include "WallTag.h"
#include "../WallModule/Wall/Wall.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace Sample;

void WallTagGraphicsElementShapeCalculator::ReportInputDataIds(std::vector<RegenDataId>& oInputDatas) const
{
    IDocument* pDocument = GetDocument();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDocument, L"获取Document失败",L"GDMP",L"2023-12-30");
    const IGenericElement* pGenericElement = quick_cast<const IGenericElement>(pDocument->GetElement(ElementId(GetRegenDataId().ObjectId)));
    DBG_WARN_AND_RETURN_VOID_UNLESS(pGenericElement, L"获取GenericElement失败",L"GDMP",L"2023-12-30");
    const WallTag* pWallTag = dynamic_cast<const WallTag*>(pGenericElement->GetExternalObject());
    DBG_WARN_AND_RETURN_VOID_UNLESS(pWallTag, L"WallTag不能为空",L"GDMP",L"2023-12-30");
    oInputDatas.emplace_back(pWallTag->GetReferenceLineDirRdId());

    IGenericElement* pGenericElement1 = quick_cast<IGenericElement>(pDocument->GetElement(pWallTag->GetWallId()));
    DBG_WARN_AND_RETURN_VOID_UNLESS(pGenericElement1, L"获取GenericElement失败",L"GDMP",L"2023-12-30");
    Wall* pWall = dynamic_cast<Wall*>(pGenericElement1->GetExternalObject());
    oInputDatas.emplace_back(pWall->GetDataRdId());
}

    void WallTagGraphicsElementShapeCalculator::Execute()
    {
        IDocument* pDocument = GetDocument();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pDocument, L"获取Document失败",L"GDMP",L"2023-12-30");
        IGenericElement* pGenericElement = quick_cast<IGenericElement>(pDocument->GetElement(ElementId(GetRegenDataId().ObjectId)));
        DBG_WARN_AND_RETURN_VOID_UNLESS(pGenericElement, L"GenericElement不能为空",L"GDMP",L"2023-12-30");
        WallTag* pWallTag = dynamic_cast<WallTag*>(pGenericElement->GetExternalObject());
        DBG_WARN_AND_RETURN_VOID_UNLESS(pWallTag, L"WallTag不能为空",L"GDMP",L"2023-12-30");
        OwnerPtr<IGraphicsElementShape> opGraphicsElementShape = pWallTag->CreateGraphicsElementShape();
        DBG_WARN_AND_RETURN_VOID_UNLESS(opGraphicsElementShape, L"GraphicsElementShape创建失败",L"GDMP",L"2023-12-30");
        opGraphicsElementShape->SetElementId(pGenericElement->GetElementId());
        IElementModelShape* pElementModelShape = pGenericElement->GetElementModelShape();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pElementModelShape, L"ElementModelShape不能为空",L"GDMP",L"2023-12-30");
        pElementModelShape->SetGraphicsElementShape(TransferOwnership(opGraphicsElementShape));
    }

    WallTagGraphicsElementShapeCalculator::WallTagGraphicsElementShapeCalculator(gcmp::IDocument * pDocument, const RegenDataId & outputDataId)
        : m_pDocument(pDocument)
        , m_outputDataId(outputDataId)
    {

    }

    std::wstring WallTagGraphicsElementShapeCalculator::GetCalculatorName() const
    {
        return L"WallGraphicsElementShapeCalculator";
    }

    gcmp::IDocument* WallTagGraphicsElementShapeCalculator::GetDocument() const
    {
        return m_pDocument;
    }

    const RegenDataId& WallTagGraphicsElementShapeCalculator::GetRegenDataId() const
    {
        return m_outputDataId;
    }